package com.linkage.seapeak.activity.chat;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.seapeak.base.BaseActivity;
import com.linkage.seapeak.fragment.ConversationSearchFragment;

public class SearchMesageActivity extends BaseActivity {

    private EditText editKeyword;
    private TextView cancel;
    private ConversationSearchFragment fragment;
    int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarMode(false);
        setContentView(R.layout.activity_search_message);
        type = getIntent().getIntExtra("type", 0);
        initView();
    }

    private void initView() {
        editKeyword = (EditText) findViewById(R.id.edit_keyword);
        cancel = (TextView) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        editKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    InputMethodManager imm = (InputMethodManager) editKeyword.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(editKeyword.getWindowToken(),0);
                    }
                    String str = editKeyword.getText().toString();
                    fragment.loadData(str);
                    return true;
                }
                return false;
            }
        });

        fragment = new ConversationSearchFragment(type);
        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment)
                .commitAllowingStateLoss();
    }
}
